<div class="container-fluid">
    <div class="wrapper">
        <div class="panel" id="nav">
            <div class="panel-heading"><h3 class="panel-title">站点调拨</h3></div>
            <div class="panel-body">
                <form id="remove-form">
                    <div class="col-md-4 form-inline">
                        <label  class="control-label" for="">SKU</label>
                        <input class="form-control add-space" name="sku" type="text">
                        <input class="btn btn-primary btn-search-from" type="button" value="查询">
                    </div>
                    <div class="col-md-3 form-inline">
                        <label class="control-label">到达站点</label>
                        <select class="chosen-select form-control" tabindex="-1" name="site_id" data-name-group="common">
                            <option value="" class="empty-opt">-- 请选择 --</option>
                            <volist name="sites" id="item">
                                <option value="{$key}">{$item}</option>
                            </volist>
                        </select>
                    </div>
                    <div class="col-md-4 form-inline">
                        <label class="control-label" style="padding-top: 5px;">中文名称:</label>
                        <span class="sku_name"></span>
                    </div>
                    <div class="clearfix"></div>
                    <table class="table table-striped table-hover table-from" cellspacing="0" width="100%">
                        <thead>
                        <tr>
                            <th><input type="checkbox" id="checkAll" /></th>
                            <th>站点</th>
                            <th>可调拨数量</th>
                            <th>调拨数量</th>
                            <th>储位</th>
                            <th>公司主体</th>
                            <th>是否退税</th>
                        </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </form>
                <div class="col-md-12 form-inline">
                    <button  class="btn btn-primary pull-right btn-sure-move" data-toggle="modal" data-target=".confirm-modal">确定移位</button>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 确定对话框 -->
<div class="modal fade confirm-modal">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">关闭</span></button>
                <h4 class="modal-title">提示信息</h4>
            </div>
            <div class="modal-body"><p class="text-primary confirm-text"></p></div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary sure-move" data-dismiss="modal">确定</button>
            <button type="button" class="btn" data-dismiss="modal">取消</button>
        </div>
    </div>
</div>

<script type="text/javascript">
    //原始储位查询
    $('.btn-search-from').click(function() {
        var sku = $.trim($('input[name="sku"]').val());
        if (sku == '') {
            layer.msg('请输入需要查询的SKU！', {icon: 5});
            return false;
        }

        $.ajax({
            type: "post",
            url: 'Warehouse/Inventory/move_inventory',
            data: {sku: sku},
            dataType: "json",
            success: function (result) {
                if (!result.status) {
                    layer.msg(result.info, {icon: 5});
                    return false;
                }

                var data = result.info;
                $(".sku_name").html(data[0]['sku_name']);
                var html = '';
                for (var i = 0; i < data.length; i++) {
                    html += '<tr style="height:50px">';
                    html += '<td><input type="checkbox" class="site" value="">' +
                            '<input type="hidden" class="check" name="check[]" value="">' +
                            '<input type="hidden" name="site[]" value="'+data[i]['site_id']+'"></td>';
                    html += '<td class="site_id">' + data[i]['site_id'] + '</td>';
                    html += '<td>' + data[i]['available_quantity'] + '</td>';
                    html += '<td>';
                    html += '<input type="number" name="available_quantity[]" class="form-control w80" value="' + data[i]['available_quantity'] + '" max="' + data[i]['available_quantity'] + '" min="1" onblur="setValue(this)" '+
                            'onkeyup="this.value=this.value.replace(/\D/g,\'\')" onafterpaste="this.value=this.value.replace(/\D/g,"")"'+'/>';
                    html += '</td>';
                    html += '<td>' + data[i]['storage_position'] +'<input type="hidden" name="storage_position[]" value="'+data[i]['storage_position']+'">'+ '</td>';
                    html += '<td>' + data[i]['company'] +'<input type="hidden" name="enterprise_dominant[]" value="'+data[i]['enterprise_dominant']+'">'+'</td>';
                    html += '<td>' + data[i]['tuisui'] +'<input type="hidden" name="export_tax_rebate[]" value="'+data[i]['export_tax_rebate']+'">'+'</td>';
                    html += '</tr>';
                }
                $('.table-from').find('tbody').html('').append(html);
            }
        });
    });

    function setValue(obj) {
        var val = $.trim($(obj).val());
        if(isNaN(val)) {
            layer.msg('移动数量只能为数字', {icon: 5});
//            $(obj).val($(obj).attr(0));
            return false;
        }
        if (parseInt(val) > parseInt($(obj).attr('max'))) {
            layer.msg('移动数量不能大于可用数量', {icon: 5});
            $(obj).val($(obj).attr('max'));
            return false;
        }

        if (parseInt(val) < parseInt($(obj).attr('min'))) {
            layer.msg('移动数量不能小于1', {icon: 5});
            $(obj).val($(obj).attr('min'));
            return false;
        }
    }

    //全选
    $('#checkAll').click(function() {
        $(".site").prop("checked", $(this).prop("checked"));
    });

    //点击确定移位
    $('.btn-sure-move').click(function() {
        var sku = $.trim($('input[name="sku"]').val());
        var toSiteId = $.trim($('select[name="site_id"]').val());

        if (sku == '') {
            layer.msg('请输入SKU！', {icon: 5});
            return false;
        }

        if (toSiteId == '') {
            layer.msg('请输入到达站点！', {icon: 5});
            return false;
        }

        var chkSite = [];
        $('.site:checked').each(function(){
            chkSite.push($(this).val());
        });

        if (chkSite.length == 0) {
            layer.msg('请至少勾选一个需要转移数量的站点！', {icon: 5});
            return false;
        }

        $('.confirm-text').html('您确定要移位吗？');
    });

    //确定移位
    $('.sure-move').click(function() {
        $(".site").each(function (i) {
            if ($(this).is(':checked')){
                $(".check").eq(i).val(1);
            } else {
                $(".check").eq(i).val(0);
            }
        });
        $.ajax({
            type: "post",
            url: 'Warehouse/Inventory/do_move_site',
            data: $('#remove-form').serialize(),
            dataType: "json",
            success: function (result) {
                if (!result.status) {
                    layer.msg(result.info, {icon: 5});
                    return false;
                }

                layer.msg(result.info, {icon: 6});
                setTimeout(function () {
                    $('.btn-search-from').click();
                    $('.btn-search-to').click();
                }, 2000);

            }
        });
    });
</script>